package nc.impl.fct.contract;

import nc.bs.framework.common.NCLocator;
import nc.itf.fct.contract.IContractService;
import nc.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.filesystem.NCFileVO;
import nc.vo.pubapp.AppContext;
import nccloud.pubitf.platform.attachment.IAttachmentService;
import nccloud.pubitf.platform.attachment.IFileStorageConst;
import uap.pub.fs.domain.basic.FileHeader;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Base64;

public class ContractServiceImpl implements IContractService {

    IAttachmentService attachmentService = NCLocator.getInstance().lookup(IAttachmentService.class);

    @Override
    public NCFileNode doUpload(String billId, String fileName, InputStream inputStream) {
        FileHeader header = attachmentService.upload(fileName, inputStream, false, 0, IFileStorageConst.Bucket);
        NCFileVO attach = new NCFileVO();
        attach.setPath(header.getName());
        attach.setCreator(AppContext.getInstance().getPkUser());
        attach.setFileLen(header.getFileSize());
        attach.setPk_doc(header.getPath());
        attach.setIsdoc("z");
        return attachmentService.saveAttachDBInfo(billId, attach);
    }

    @Override
    public NCFileNode doUpload(String billId, String fileName, String base64Str) {
        byte[] decodedBytes = Base64.getDecoder().decode(base64Str);
        return doUpload(billId, fileName, new ByteArrayInputStream(decodedBytes));
    }
}
